<?php

/**
 * This is the model class for table "project".
 *
 * The followings are the available columns in table 'project':
 * @property string $id
 * @property integer $userId
 * @property string $name
 * @property string $type
 * @property string $province_id
 * @property double $finance_range
 * @property string $partners
 * @property string $file_path
 * @property integer $status
 * @property string $created_date
 * @property string $updated_date
 *
 * The followings are the available model relations:
 * @property Province $province
 * @property Users $user
 */
class Project extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Project the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'project';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			// array('file_path', 'required', 'on' => 'create'),
			array('name, province_id, finance_range, partners', 'required'),
			array('userId, status', 'numerical', 'integerOnly'=>true),
			array('finance_range', 'numerical'),
			array('name, partners', 'length', 'max'=>50),
			array('province_id', 'length', 'max'=>10),
			// array('file_path', 'length', 'max'=>500),
			// array('file_path','ext.upload.Upload','statusVar'=>'file_status','allowEmpty'=>false,'savePath'=>'upload/','types'=>"pdf,doc"),
			array('file_path', 'file', 'types'=>'doc, pdf', 'allowEmpty'=>true , 'on' => 'save'),
			array('file_path', 'file', 'types'=>'doc, pdf', 'on' => 'create'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, userId, name, type, province_id, finance_range, partners, file_path, status, file_status, created_date, updated_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'province' => array(self::BELONGS_TO, 'Province', 'province_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'userId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'userId' => 'User',
			'name' => 'Name',
			'province_id' => 'Province',
			'finance_range' => 'Finance Range',
			'partners' => 'Partners',
			'file_path' => 'File',
			'status' => 'Status',
			'created_date' => 'Created Date',
			'updated_date' => 'Updated Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		if(Yii::app()->user->checkAccess('member')){
			$criteria->condition = "userId= " .Yii::app()->user->id;
		}
		$criteria->compare('id',$this->id,true);
		$criteria->compare('userId',$this->userId);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('province_id',$this->province_id,true);
		$criteria->compare('finance_range',$this->finance_range);
		$criteria->compare('partners',$this->partners,true);
		$criteria->compare('file_path',$this->file_path,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('updated_date',$this->updated_date,true);
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}